package com.muchfish.reactor.example;

import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

public class ReactorOptimizedExample {
    public static void main(String[] args) {
        Mono.fromCallable(() -> {
                    // 模拟异步操作
                    Thread.sleep(2000);
                    return "First operation completed";
                })
                .subscribeOn(Schedulers.boundedElastic())
                .flatMap(result -> {
                    System.out.println("First Reactor: " + result);
                    return Mono.fromCallable(() -> {
                        // 模拟异步操作
                        Thread.sleep(2000);
                        return "Second operation completed with input: " + result;
                    }).subscribeOn(Schedulers.boundedElastic());
                })
                .flatMap(result -> {
                    System.out.println("Second Reactor: " + result);
                    return Mono.fromCallable(() -> {
                        // 模拟异步操作
                        Thread.sleep(2000);
                        return "Third operation completed with input: " + result;
                    }).subscribeOn(Schedulers.boundedElastic());
                })
                .doOnSuccess(result -> System.out.println("Third Reactor: " + result))
                .doOnError(error -> System.out.println("Error in Reactor: " + error.getMessage()))
                .block(); // 阻塞等待操作完成

        // 主线程继续执行其他操作
        System.out.println("Main thread continues...");
    }
}


